#!c:/Perl/bin/perl
# (c) 2009, nizamov@inbox.ru

	use LWP 5.64;
	use HTTP::Cookies; 
	use HTTP::Request::Common;
	use URI::Escape;

#	my $use_proxy = 'http://121.22.29.182:80'; # proxy address here
	
	my $login_mail = '';
	my $login_pass = '';
	# URL we want to post to
	my $url_post = '';
	
	my $msg_file = './quotes.txt';
	
	# Quotes delimiter. 
	# To separate qoutes with ONE new line: $delim = "\r\n"; (or "\n" in UNIX EOL mode)
	# To separate qoutes with TWO new lines: $delim = "\r\n\r\n"; (or "\n\n" in UNIX EOL mode)
	# To separate qoutes with any symbol: $delim = "_symbol_";
	# Note that you must put "\"  before Perl's special symbols: ", ', (, ), [, ], and others
	my $delim = "\r\n\r\n";
	my $repeat = 10000000; # repeat count
	my $delay = 4; # 4 is minimum delay for walls,albums and boards.  5 - for mail
	my $postfix = 1; # set 1 if you want to post many equal messages
	
	my $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1;) Firefox/2.0.0.0';
	my $url_login = 'http://vkontakte.ru/login.php';
	
	my $ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => [ 'POST', 'GET' ]);
	$ua->proxy('http', $use_proxy) if $use_proxy;
	$ua->agent($useragent);
	$ua->cookie_jar({});
	
	print 'Login... ';
	my $response = $ua->post($url_login, 
		[	'email' => $login_mail, 
			'pass' => $login_pass, 
		]);
	die 'Login error -- ', $response->status_line
		unless $response->is_success or $response->code == 302;
	print "Success.\n";
	srand;
	
	print 'Load quotes... ';
	my $buff;
	my $filedata;
	open(MSG, $msg_file) or die "open error!";;
	binmode MSG;
	while (read(MSG, $buff, 1024)) { $filedata .= $buff; }
	@messages = split $delim, $filedata;
	print "Success...\n";
	
	print 'Get URL content... ';
	$response = $ua->get($url_post);
	die 'Get page error -- ', $response->status_line
		unless $response->is_success or $response->code == 302;
	print "Success.\n";
	my $cont = $response->content;
#	open (LOG, ">log.html");
#	print LOG $cont;
#	close (LOG);
	my $url = $url_post;
	my $res; my $app='';

L0:	foreach $message (@messages) {
		# add random number to the end of the message
		$app = int(rand(999999)) if $postfix;
		
		$_ = $cont;
		if ($cont =~ /action\s*=\s*\"wall.php\"\s*name\s*=\s*\"postMessage\"\s*id\s*=\s*\"postMessage\"/) {
		print 'Comment on the wall... ';
			$url = 'http://vkontakte.ru/wall.php' ;
			($id_value) = /name\s*=\s*\"to_id\"\s*value\s*=\s*\"(\w+)\"/;
			($wall_hash) = /wall_hash: '(\w+)\'/;
			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						act => 'get10',
						wall_hash => $wall_hash,
						wpage => '100000',
						seed => "$message" . "\n$app",
						fpage => '1',
						mid => $id_value,
						n =>'3'
					];
		} elsif ($cont =~ /id\s*=\s*\"comment\"\s*name\s*=\s*\"comment\"\s*action\s*=\s*\'photos.php\'/) {
			print 'Comment on the photo... ';
			$url = 'http://vkontakte.ru/photos.php';
			($id_value) = /name\s*=\s*\"id\"\s*value\s*=\s*\"([^\"]+)\"/;
			($aid) = /name\s*=\s*\"aid\"\s*value\s*=\s*\"(\d+)\"/;
			($hash) = /name\s*=\s*\"hash\"\s*value\s*=\s*\"(\w+)\"/;
			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						act => 'a_post_comment',
						add_bookmark => '1',
						aid => $aid,
						comment => "$message" . "\n$app",
						hash => $hash,
						id => $id_value,
					];
		} elsif ($cont =~ /action=\"javascript:\s*submitTopic\(\)\"\s*name=\"editPost\"\s*id=\"editPost\"/) {
			print 'New Discussion in the group or WTF it was before?... ';
			($id_value) = /name\s*=\s*\"gid\"\s*value\s*=\s*\"(\d+)\"/;
			($url) = 'http://vkontakte.ru/board.php?act=newtopic&id=' . $id_value;
			($hash) = /name\s*=\s*\"hash\"\s*value\s*=\s*\"(\w+)\"/;
			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						act => 'a_new_topic',
						gid => $id_value,
						hash => $hash,
						title => substr($message,0,32),
						post => "$message" . "\n$app",
					];
		} elsif ($cont =~ /id=\"postmessage\"\s*name=\"postmessage\"\s*action=\'javascript:\s*post_answer\(\)\'/) {
			print 'Comment in discussion in the group or WTF it was before?... ';
			($id_value) = /name\s*=\s*\"tid\"\s*value\s*=\s*\"(\d+)\"/;
			$url = 'http://vkontakte.ru/board.php';
			($hash) = /name\s*=\s*\"hash\"\s*value\s*=\s*\"(\w+)\"/;
			($gid) = /name\s*=\s*\"gid\"\s*value\s*=\s*\"(\d+)\"/;
			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						act => 'a_post_answer',
						tid => $id_value,
						post => "$message" . "\n$app",
						hash => $hash,
						gid => $gid,
						add_bookmark => '1'
					];
		} elsif ($cont =~ /action=\"javascript:\s*send_message\(\)\"\s*name=\"postMessage\"\s*id=\"postMessage\"/) {
			print 'Mail message to contact... ';
			$url = 'http://vkontakte.ru/mail.php';
			($id_secure) = /name\s*=\s*\"secure\"\s*value\s*=\s*\"(\w+)\"/;
			($chas) = /name\s*=\s*\"chas\"\s*value\s*=\s*\"(\w+)\"/;
			($id_photo) = /name\s*=\s*\"photo\"\s*value\s*=\s*\"(\w+)\"/;
			($id_value) = /name\s*=\s*\"to_id\"\s*value\s*=\s*\"(\d+)\"/;
			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						act => 'sent',
						secure => $id_secure,
						chas => $chas,
						photo => $id_photo,
						to_id => $id_value,
						to_reply => '0',
						title => substr($message,0,32),
						message => "$message" . "\n$app",
					];
		} elsif ($cont =~ /name=\"login\"\s*id=\"login\"\s*action=\"\/login.php\"/) {
			print 'Authorization error?... Please repeat later... \n Exit!';
			exit;
		} else {
			print 'WTF page on you want to post?... \n Exit!';
			exit;
		}
		
		print 'Send... ';
		$response = $ua->request($req);
		do { warn 'Post error -- ', $response->status_line; next; }
			unless $response->is_success or $response->code == 302;
		print "Success.\n";
		sleep $delay;
	}
	
	goto L0 if ($repeat--);